package com.hjh.aop.demo1;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;
import java.util.List;

public class MethodInvocation1 implements MethodInvocation {

    private List<MethodInterceptor> chain;
    private Object target;
    private Method method ;
    private Object[] args;
    private int currentChainSize = 0;

    public MethodInvocation1(List<MethodInterceptor> chain,Object target,Method method ,Object[] args){
        this.chain = chain;
        this.target = target;
        this.method = method;
        this.args =args;
    }



    @Override
    public Object proceed() throws Throwable {
        if(currentChainSize == chain.size()){
            return method.invoke(target,args);
        }

        MethodInterceptor methodInterceptor = chain.get(currentChainSize++);
        return methodInterceptor.invoke(this);
    }

    @Override
    public Object getThis() {
        return this;
    }

    @Override
    public AccessibleObject getStaticPart() {
        return null;
    }

    @Override
    public Method getMethod() {
        return method;
    }

    @Override
    public Object[] getArguments() {
        return args;
    }
}
